namespace ghost {
    /**
     // 多选列表 和 item内部渲染
        class MultiListView extends Laya.ui.view {
            constructor(){
                super();
                this.list1 = new ly.BaseList; // 	Laya.List
                this.list1.name = "list1"
                this.list1.itemRender = ListItem;
                this.list1.setMultiSelect()// 设置多选
                this.list1.width = 128;
                this.list1.height = 299;
                let data = this.list1.selectedData; // 多选数据
            }
        }

        class ListItem extends ItemRenderer {
            // 重写这个内部渲染函数
            protected onRender(){
                let data = this._data
            }
        }
    */
    export class BaseList extends Laya.List {
        /**当前选择的Cell*/
        protected _curSelectCell: any;
        /**当前选择的Data*/
        protected _curSelcetData: any;
        /* 是否多选 */
        protected _multiSelect = false;
        /* 多选数组 */
        protected _selectedArray: boolean[];
        private _clickConditionHandle: Laya.Handler

        public animDelItem(index: number) {

        }

        /* 设置多选 */
        public setMultiSelect() {
            this._multiSelect = true;
            this._selectedArray = [];
        }


        public isSelected(index: number) {
            if (this._multiSelect) {
                return this._selectedArray[index];
            }
        }


        /**
         * 获取选中数据
         * 支持单选多选的数据返回
        */
        public get selectedData() {
            let s = this;
            if (!s._multiSelect) {
                return s.selectedItem
            }
            else {
                if (s._selectedArray) {
                    let arr: any[] = [];

                    for (let i: number = 0, len: number = s._selectedArray.length; i < len; ++i) {
                        if (s._selectedArray[i]) {
                            let data = this._array[i]
                            arr.push(data)
                        }
                    }
                    return arr
                }
                return null
            }
        }

        public clear() {
            if (this._multiSelect) {
                this._selectedArray.length = 0;
            }
            this.refresh()
        }

        // public refresh() {
        //     // 如果是多选，要清一下多选状态
        //     if (this._multiSelect) {
        //         this._selectedDic = {}
        //     }
        //     super.refresh()
        // }

        //@ts-ignore
        public get array(): any[] {
            return this._array;
        }
        public set array(value: any[]) {
            if (this._multiSelect) {
                this._selectedArray.length = 0;
            }
            this.runCallLater(this.changeCells);
            this._array = value || [];
            var length = this._array.length;
            this.totalPage = Math.ceil(length / (this.repeatX * this.repeatY));
            this._selectedIndex = this._selectedIndex < length ? this._selectedIndex : length - 1;
            this.startIndex = this._startIndex;
            if (this._scrollBar) {
                this._scrollBar.stopScroll();
                var numX = this._isVertical ? this.repeatX : this.repeatY;
                var numY = this._isVertical ? this.repeatY : this.repeatX;
                var lineCount = Math.ceil(length / numX);
                var total = this._cellOffset > 0 ? this.totalPage + 1 : this.totalPage;
                if (total > 1) {
                    this._scrollBar.scrollSize = this._cellSize;
                    this._scrollBar.thumbPercent = numY / lineCount;
                    this._scrollBar.setScroll(0, (lineCount - numY) * this._cellSize + this._cellOffset, this._scrollBar.value);
                    this._scrollBar.target = this._content;
                } else {
                    this._scrollBar.setScroll(0, 0, 0);
                    this._scrollBar.target = this._content;
                }
            }
        }
        /**
         * 设置多选状态
         * example  selectList = [true,false,false...];//索引对应数据索引
        */
        public set selectList(val: boolean[]) {
            this._selectedArray = val
            this.changeSelectStatus();
            // this.runCallLater(this.changeSelectStatus);
            // Laya.timer.callLater(this, this.changeSelectStatus)
        }
        /**
         * 获取多选状态
         * example return {0：true, 1:true}
        */
        public get selectList(): boolean[] {
            if (!this._multiSelect) {
                return null
            }
            return this._selectedArray;
        }

        /**
         * 重写changeSelectStatus函数
         */
        protected changeSelectStatus() {
            let self = this;
            let dataInd: number;
            let data: any;
            for (var i = 0, n = self._cells.length; i < n; i++) {
                if (self._multiSelect) {
                    dataInd = self._startIndex + i;
                    self.changeCellState(self._cells[i], self._selectedArray[dataInd], 1);
                }
                else {
                    self.changeCellState(self._cells[i], self._selectedIndex === self._startIndex + i, 1);
                }
            }
        }


        protected changeCellState(cell, visable, index) {
            super.changeCellState(cell, visable, index)
            cell.selected = visable;
        }

        protected multiSelect(value: number): void {
            let self = this;
            // if (!self._selectedArray[value]) {
            //     self._selectedArray[value] = true
            // }
            // else {
            //     delete self._selectedArray[value] // = !self._selectedDic[value]
            // }
            //判断是否能勾选
            if (!self._selectedArray[value] && this._clickConditionHandle && !this._clickConditionHandle.runWith(value)) {
                return;
            }
            self._selectedArray[value] = !self._selectedArray[value];
            self.changeSelectStatus();
            self.event(Laya.Event.CHANGE);
            self.selectHandler && self.selectHandler.runWith(value);
        }

        /**
        * 重写 onCellMouse
        * 单元格的鼠标事件侦听处理函数。
        */
        protected onCellMouse(e: Laya.Event) {
            let self = this;
            if (e.type === Laya.Event.MOUSE_DOWN) {
                self._isMoved = false;
            }
            var cell = e.currentTarget as Laya.Box;
            var index = self._startIndex + self._cells.indexOf(cell);
            if (index < 0) return;
            if (e.type === Laya.Event.CLICK || e.type === Laya.Event.RIGHT_CLICK) {
                if (self.selectEnable && !self._isMoved) {
                    if (self._multiSelect) {
                        self.multiSelect(index)
                    }
                    else {
                        self.selectedIndex = index;
                    }
                } else {
                    self.changeCellState(cell, true, 0);
                }
            } else if ((e.type === Laya.Event.MOUSE_OVER || e.type === Laya.Event.MOUSE_OUT) && self._selectedIndex !== index) {
                if (!self._multiSelect) {
                    self.changeCellState(cell, e.type === Laya.Event.MOUSE_OVER, 0);
                }
                else {
                    // self.changeCellState(cell, e.type === Laya.Event.MOUSE_OVER, 0);
                    self.changeCellState(cell, e.type === Laya.Event.MOUSE_OVER || self._selectedArray[index], 0);
                }
            }
            self.mouseHandler && self.mouseHandler.runWith([e, index]);
        }
        protected useDefaultRenderHandler(): void {
            let s = this;
            if (s.renderHandler)
                s.renderHandler.recover();
            s.renderHandler = Laya.Handler.create(s, function (cell: any, index: number): void {
                cell.index = index;
                cell.setData(s._array[index]);
                if (cell.getData() == s._curSelcetData) {
                    if (s._curSelectCell)
                        s._curSelectCell.selected = false;
                    s._curSelectCell = cell;
                    cell.selected = true;
                }
                else if (cell.selected)
                    cell.selected = false;
            }, null, false);
        }
        protected useDefaultMouseHandler(): void {
            let s = this;
            if (s.mouseHandler)
                s.mouseHandler.recover();
            s.mouseHandler = Laya.Handler.create(s, function (e: Laya.Event, index: number): void {
                let cell: any;
                if (e.type == Laya.Event.CLICK) {
                    if (s._curSelectCell) {
                        s._curSelectCell.selected = false;
                        s._curSelcetData = s._curSelectCell = null;
                    }
                    cell = <any>e.currentTarget;
                    cell.selected = true;
                    s._curSelectCell = cell;
                    s._curSelcetData = s._array[index];
                }
            }, null, false);
        }
        public useDefaultHandle(): void {
            let s = this;
            s.useDefaultRenderHandler();
            s.useDefaultMouseHandler();
        }

        public destory(destroyChild?: boolean) {
            super.destroy(destroyChild);
            this._selectedArray = null;
            this._clickConditionHandle = null;
        }

        public setClickCondition(handle: Laya.Handler) {
            this._clickConditionHandle = handle;
        }

    }


}
